package test.处理;

import cn.hutool.core.io.FileUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.DSFactory;
import cn.hutool.db.ds.druid.DruidDSFactory;
import com.alibaba.fastjson.JSON;
import entity.Pubmed;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import utils.SmallTool;
import utils.StringUtil;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.IntStream;

/**
 * @Author: gcr
 * @Date: 2022年5月14日13:52:18
 */
public class 比对老板网页数据单独比对出版时间20220514 {

    @SneakyThrows
    public static void main(String[] args) {
        String dbNameMy = "pubmed.pubmed_vip_extend_0328_33771437";
        String dbNameBoss = "pmid_doi_down_all_0328_33900965";

        int effectSize = 0;

        int startPmid = 0;
        int endPmid = 36000000;
        int breakSize = 100000;


        //存放比对结果
        Map<String, Integer> resMapConpare = new ConcurrentHashMap<>();

        // 用于存放doi 只有一方有的情况 打印出来
        Map<Integer, String> pmidDoi = new LinkedHashMap<>();
        // 双方doi不同
        Map<Integer, String> pmidDoiNoSame = new LinkedHashMap<>();


        SmallTool.printMemory();
        DSFactory.setCurrentDSFactory(new DruidDSFactory());
        DataSource ds = DSFactory.get("server_local");
//        DataSource ds2 = DSFactory.get("server_local3307");
        Db use = Db.use(ds);
        Db use3307 = Db.use(ds);

        // 需要对比的数据如下
        //doi	  `citationDoi` varchar(90) DEFAULT NULL,
        //mh-mhy	  `meshSet` text,
        //pm	  `pmid` int(8) DEFAULT NULL,
        //Review	  `typeSet` text,
        //Substances	  `subSet` text,
        //标题	  `citationTitle` text,
        //关键词	  `keyWords` text,
        //来源	  `citationJournalTitle` varchar(120) DEFAULT NULL,
        //年卷期-页码	  `text` text,
        //未确认	  `linkSet` text,
        //未确认	  `figure` longtext,
        //摘要	  `abs` longtext,
        //作者	  `authorMap` text,
        //作者地址	  `addressMap` longtext,

        String allDate = "select max(pmid) as pmid  from " + dbNameBoss;
        Entity query = use3307.query(allDate).get(0);
        SmallTool.printMess("最大的id 是" + query.getStr("pmid"));

//        int startPmid = 2000001;
//        int endPmid = 3000000;

        Integer count = query.getInt("pmid");
        int pageSize = count / breakSize + 1;
        SmallTool.printMess("全部页码+++++++++" + pageSize);
//        SmallTool.printMess("即将开始请提前关闭如果想的话十秒倒计时");
//        SmallTool.SleepMillis(10000L);
        SmallTool.printMess("任务开始+++++++++++++++++++++++++++++++++++++++++++++++++++++");
        IntStream.rangeClosed(1, pageSize).parallel().forEach(i -> {
            int start = breakSize * (i - 1) + 1;
            int end = i * breakSize;
            SmallTool.printMess("开始id++" + start + "+++结束id++++" + end);
            if (end <= startPmid) {
//                SmallTool.printMess("因为小于" + startPmid + " 略过了");
                return;
            }

            if (end > endPmid) {
//                SmallTool.printMess("因为小于" + startPmid + " 略过了");
                return;
            }
            SmallTool.printMess("当前页码+++++" + i);
            SmallTool.printMemory();

            String boss = "select pmid ,text from " + dbNameBoss + " where pmid between " + start + " and " + end;
            String me = "select pmid,pub_date,pub_year,medline_date  from " + dbNameMy + " where pmid between " + start + " and " + end;
            List<Entity> queryBoss = null;
            try {
                queryBoss = use3307.query(boss);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            Map<Integer, Entity> bossMap = new HashMap<>();
            for (Entity entity : queryBoss) {
                bossMap.put(entity.getInt("pmid"), entity);
            }
            List<Pubmed> queryMe = null;
            try {
                queryMe = use.query(me, Pubmed.class);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            // 相比对 doi pmid 标题
            queryMe.parallelStream().forEach(pmDeatil -> {
                Integer pmid = pmDeatil.getPmid();
                String pubDate = pmDeatil.getPubDate();
                String pubYear = pmDeatil.getPubYear();
                String pubDateMedline = pmDeatil.getMedlineDate().split(" ")[0];
                if (bossMap.containsKey(pmid)) {
                    Entity entityBoss = bossMap.get(pmid);


                    String ISSusBoss = preDealStr(entityBoss.getStr("text"));
                    if (ISSusBoss != null) {
                        //情况太多来的 值匹配年和和后面的数据 其他不看了
                        String[] splitBoss = ISSusBoss.split(";");
                        String splitBoss1 = splitBoss[0];
                        String boosNian = "";
                        String[] s1 = splitBoss1.split(" ");
                        if (s1.length > 1) {
                            try {
                                boosNian = s1[1];
                                Integer.parseInt(boosNian);
                            } catch (Exception e) {
                                boosNian = s1[0];
                            }

                        } else {
                            boosNian = s1[0].trim();
                        }

                        if (!boosNian.equals(pubYear)) {

                            String STR = "年份不一致++boss++" + boosNian + "++meyear++" + pubYear + "++memedline++" + pubDateMedline + "++pmid++" + pmid + "\n";
//                          SmallTool.printMessError(STR);
                            FileUtil.appendString(STR, "/usr/local/20220514/比对出版年记录.log", "UTF-8");
                            // 进行比对带有medline的日期
                            String myMedlineYear = pubDateMedline.split(" ")[0].trim();

                        }
                    } else {
                        String STR = "年份不一致++boss++" + "kong" + "++meyear++" + pubYear + "++memedline++" + pubDateMedline + "++pmid++" + pmid + "\n";
//                          SmallTool.printMessError(STR);
                        FileUtil.appendString(STR, "/usr/local/20220514/比对出版年记录.log", "UTF-8");
                    }


                } else {
                    String STR = "年份不一致++boss++" + "kong" + "++meyear++" + pubYear + "++memedline++" + pubDateMedline + "++pmid++" + pmid + "\n";
                    FileUtil.appendString(STR, "/usr/local/20220514/比对出版年记录.log", "UTF-8");
                    //SmallTool.printMessError("老板不存在pmid+++" + pmid);
                    handleCompareSize(resMapConpare, "老板不存在pmid+++");
                }
//                SmallTool.printMess("结束比对pmid+++++++++++++++++++" + pmid);
            });
//            for (Pubmed pmDeatil : queryMe) {
//
//            }
        });

        for (int i = 1; i <= pageSize; i++) {

        }
        SmallTool.printMess("对比总数++" + effectSize);
        SmallTool.printMess(resMapConpare);
        SmallTool.printMess("+++doi一方有++" + JSON.toJSONString(pmidDoi));
        SmallTool.printMess("++++doi不同+++" + JSON.toJSONString(pmidDoiNoSame));

    }

    // 处理标题
    public static String handleStr(String str) {
        return DelOther(str);
    }

    // 预处理老板拿到的时间 因为部分时间是这样的
    // 2015 -2016;201(6):445-463.

    public static String preDealStr(String s) {
        if (StringUtils.isNotEmpty(s)) {
            s = s.replace(" -", "-");
            return s;
        } else {

            return "";
        }


    }

    // 处理卷期页 与老板一致进行比对
    public static String handleIssue(String str) {
        if (StringUtil.isNull(str)) {
            return str;
        } else {
            String[] s = str.split(" ");
            StringBuffer stringBuffer = new StringBuffer();
            for (int i = 0; i < s.length; i++) {
                if (i == 2) {
                    boolean b = s[i].startsWith("0");
                    if (b) {
                        String replace = s[i].replace("0", "");
                        stringBuffer.append(replace);
                    } else {
                        stringBuffer.append(s[i]);
                    }

                } else {
                    stringBuffer.append(s[i] + " ");
                }
            }
            return stringBuffer.toString();

        }


    }

    public static String DelOther(String str) {
        return str.replaceAll("[^a-zA-Z0-9\\u4E00-\\u9FA5]", "").toUpperCase();
    }

    // 2022年3月16日13:47:20 转换成网页显示的mesh词格去掉*
    public static List<String> parseMesh(List<String> meshBoss) {
        List<String> res = new LinkedList<>();
        for (String boss : meshBoss) {
            res.add(boss.replace("*", ""));
        }
        return res;

    }


    public static Set<String> getErrorPts() {
        Set<String> pst = new HashSet<>();
        pst.add("Journal Article");
        return pst;
    }

    public static void handleCompareSize(Map<String, Integer> res, String param) {
        res.compute(param, (k, v) -> {
            if (v == null) {
                v = 1;
            } else {
                v = v + 1;
            }
            return v;

        });
    }


    // 存放pmid 对应的doi
    public static void handleCompareSizePmidDoi(Map<Integer, String> res, Integer pmid, String doi) {
        res.put(pmid, doi);

    }

}
